﻿using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace Grinder.Settings
{
    [Serializable]
    public sealed class SectionNotFoundException : Exception
    {
        private readonly string _section;

        public string Section { get { return _section; } }

        public override string Message
        {
            get
            {
                if (string.IsNullOrEmpty(_section)) return base.Message;
                var msg = new StringBuilder(base.Message);
                msg.Append(_section);
                return msg.ToString();
            }
        }

        public SectionNotFoundException() { }
        public SectionNotFoundException(string message) : base(message) { }
        public SectionNotFoundException(string message, Exception innerException) : base(message, innerException) { }

        public SectionNotFoundException(string message, string section) :
            this(message) { _section = section; }

        public SectionNotFoundException(string message, string section, Exception innerException) :
            this(message, innerException) { _section = section; }

        private SectionNotFoundException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            _section = info.GetString("Section");
        }

        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("Section", _section);
        }
    }
}
